Below, you’ll find a detailed guide on how to add the PJ News Ticker Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the PJ News Ticker Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the PJ News Ticker Plugin and the shortcodes it provides:
"PJ News Ticker is a dynamic WordPress plugin that allows users to display a news ticker on their website. It's ideal for highlighting the latest news, updates, and important announcements in a scrolling format."
- [pj-news-ticker]
PJ News Ticker [pj-news-ticker] Shortcode
The PJ News Ticker shortcode is a tool that fetches and displays a set number of posts in a ticker format. It allows customization like post type, speed, font, and colors.
Shortcode: [pj-news-ticker]
Parameters
Here is a list of all possible pj-news-ticker shortcode parameters and attributes:
num_posts
– It sets the number of posts to display.post_type
– It determines the type of post to show.post_cat
– It specifies the category of posts to display.hide_if_empty
– If ‘true’, hides the ticker when no posts are available.google_font_choice
– If ‘custom’, uses a Google font specified by URL.google_font
– Specifies the Google font to use for the ticker.show_label
– If ‘true’, displays a label before the ticker.label_bg_colour
– Sets the background color of the label.label_text_colour
– Sets the color of the label text.size
– Sets the font size for the ticker.no_content_text
– Text to display when there are no posts.override_posts
– If ‘true’, overrides the default posts with custom text.custom_separator_condition
– If ‘true’, uses a custom image as the separator.custom_separator
– Specifies the custom separator to use.custom_separator_image
– Specifies the image to use as the separator.custom_separator_width
– Sets the width of the custom separator image.override_posts_custom_text
– The custom text to display when overriding posts.override_posts_custom_text_url
– The URL to link the custom text to when overriding posts.override_posts_custom_text_colour
– The color of the custom text when overriding posts.
Examples and Usage
Basic example – Displaying a news ticker with default settings
[pj-news-ticker /]
In this basic example, the shortcode will display a news ticker using the default settings. The ticker will display the latest posts from all categories in a descending order.
Advanced example – Customizing the news ticker
[pj-news-ticker num_posts="5" post_type="event" post_cat="sports" show_label="true" label_text="Latest Sports Events" /]
In this advanced example, we are customizing the news ticker. We are setting the number of posts to display to 5, changing the post type to ‘event’, and specifying the category as ‘sports’. We are also enabling the label and setting its text to ‘Latest Sports Events’.
Another Advanced example – Using a custom Google font and customizing the ticker appearance
[pj-news-ticker google_font_choice="custom" google_font_url="https://fonts.googleapis.com/css?family=Roboto" label_bg_colour="#000000" label_text_colour="#ffffff" ticker_bg_colour="#ffffff" /]
In this example, we are using a custom Google font (Roboto) for the news ticker. We are also customizing the appearance of the ticker by setting the background color of the label to black, the text color of the label to white, and the background color of the ticker to white.
PHP Function Code
In case you have difficulties debugging what causing issues with [pj-news-ticker]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('pj-news-ticker', array($this, 'renderNewsTicker'));
Shortcode PHP function:
function renderNewsTicker($atts = array(), $content = null)
{
// Override default options with the shortcode options
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$options = shortcode_atts($this->defaultOptions, $atts);
// Get the posts
$postOptions = array(
'numberposts' => $options['num_posts'],
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $options['post_type'],
);
if (!empty($options['post_cat']))
{
$postOptions['category_name'] = $options['post_cat'];
}
$posts = get_posts($postOptions);
// return early if no posts and option to hide is set
if (count($posts) == 0 && $options['hide_if_empty'] == 'true') {
return $content;
}
if($options['google_font_choice'] === 'custom') {
$font_family = $options['google_font_url'];
if ( strpos( $font_family, '&' ) !== false ) {
preg_match( '~=(.*?)&~', $font_family, $font_family );
$font_family = str_replace( "+", " ", $font_family[1] );
if ( strpos( $font_family, ':' ) !== false ) {
$font_family = strstr( $font_family, ':', true );
}
} else {
$font_family = substr( $font_family, strpos( $font_family, "=" ) + 1 );
if ( strpos( $font_family, ':' ) !== false ) {
$font_family = strstr( $font_family, ':', true );
}
if ( strpos( $font_family, "+" ) !== false ) {
$font_family = str_replace( "+", " ", $font_family );
}
}
} else {
$font_family = $options['google_font'];
}
// Render the marquee
if ($options['show_label'] == 'true')
{
$content .= '<div class="pjnt-border" style="font-family: '. $font_family .', sans-serif; background-color: '.$options['label_bg_colour'].'; border-color: '.$options['label_bg_colour'].';">';
$content .= '<div class="pjnt-label" style="color: '.$options['label_text_colour'].'; font-size: '.$options['size'].';">'.$options['label_text'].'</div>';
}
$content .= '<div class="pjnt-box" style="background-color: '.$options['ticker_bg_colour'].';">';
$content .= '<div class="pjnt-content" data-gap="'.$options['gap'].'" data-speed="'.$options['speed'].'" style="font-size: '.$options['size'].';">';
if (count($posts) == 0)
{
$content .= '<span class="pjnt-item">';
$content .= $options['no_content_text'];
$content .= '</span>';
}
else
{
if($options['override_posts'] == 'false') {
$isFirst = true;
foreach ( $posts as $post ) {
if($options['custom_separator_condition'] == 'false'){
if($options['custom_separator'] !== ''){
$content .= '<span class="pjnt-separator">'. $options['custom_separator'] . '</span>';
}
} else {
if($options['custom_separator_image'] !== ''){
$content .= '<span class="pjnt-separator"><img src="' . $options['custom_separator_image'] . '" style="max-width:' . $options['custom_separator_width'] .'; "></span>';
}
}
$content .= '<span class="pjnt-item">';
if($options['override_posts_custom_colour'] == 'false') {
$content .= '<a target="' . $options['target'] . '" href="' . get_permalink( $post ) . '">' . get_the_title( $post );
}
else {
$content .= '<a target="' . $options['target'] . '" style="color: ' . $options['override_posts_custom_text_colour'] . ';" href="' . get_permalink( $post ) . '">' . get_the_title( $post );
}
if ( $options['show_excerpt'] == 'true' ) {
setup_postdata( $post );
$content .= ' - ' . get_the_excerpt( $post );
}
$content .= '</a>';
$content .= '</span>';
}
} else {
if($options['override_posts_custom_text_url'] !== '' && $options['override_posts_custom_text_url'] !== 'Invalid URL') {
if($options['custom_separator_condition'] == 'false'){
if($options['custom_separator'] !== ''){
$content .= '<span class="pjnt-separator">'. $options['custom_separator'] . '</span>';
}
} else {
if($options['custom_separator_image'] !== ''){
$content .= '<span class="pjnt-separator"><img src="' . $options['custom_separator_image'] . '" style="max-width:' . $options['custom_separator_width'] .'; "></span>';
}
}
$content .= '<span class="pjnt-item">';
if($options['override_posts_custom_colour'] == 'false'){
$content .= '<a target="' . $options['target'] . '" href="' . $options['override_posts_custom_text_url'] . '">' . $options['override_posts_custom_text'];
} else {
$content .= '<a target="' . $options['target'] . '" style="color: ' . $options['override_posts_custom_text_colour'] . ';" href="' . $options['override_posts_custom_text_url'] . '">' . $options['override_posts_custom_text'];
}
$content .= '</a>';
$content .= '</span>';
} else {
if($options['override_posts_custom_colour'] == 'false'){
if($options['custom_separator_condition'] == 'false'){
if($options['custom_separator'] !== ''){
$content .= '<span class="pjnt-separator">'. $options['custom_separator'] . '</span>';
}
} else {
if($options['custom_separator_image'] !== ''){
$content .= '<span class="pjnt-separator"><img src="' . $options['custom_separator_image'] . '" style="max-width:' . $options['custom_separator_width'] .'; "></span>';
}
}
$content .= '<span class="pjnt-item">';
} else {
if($options['custom_separator_condition'] == 'false'){
if($options['custom_separator'] !== ''){
$content .= '<span class="pjnt-separator">'. $options['custom_separator'] . '</span>';
}
} else {
if($options['custom_separator_image'] !== ''){
$content .= '<span class="pjnt-separator"><img src="' . $options['custom_separator_image'] . '" style="max-width:' . $options['custom_separator_width'] .'; "></span>';
}
}
$content .= '<span class="pjnt-item" style="color: ' . $options['override_posts_custom_text_colour'] . ';">';
}
$content .= $options['override_posts_custom_text'];
$content .= '</span>';
}
}
}
$content .= '</div>'; // end content
$content .= '</div>'; // end marquee box
if ($options['show_label'] == 'true')
{
$content .= '</div>'; // end border container
}
return $content;
}
Code file location:
pj-news-ticker/pj-news-ticker/pj-news-ticker.php
Conclusion
Now that you’ve learned how to embed the PJ News Ticker Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply