Styling RSS feed? - wordpress

I'm trying to style the RSS feed that the user would see. So all all they would see is the image from the post, the text from the WYSIWYG editor and a button that's part of the theme.
Here is the button from the single.php template file:
<div class="bottomgift clearfix">
<a target="_blank" rel="nofollow" href="<?php getCustomField('Affiliate Link'); ?>" class="go-button">Go</a>
</div>
How do I add all this to style the RSS feed?
In Functions.php
function modRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'full', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
}
$content = $content . 'Go';
return $content;
}
add_filter('the_excerpt_rss', 'modRSS');
add_filter('the_content_feed', 'modRSS');

You can prepend or amend any syntax to WordPress RSS feeds quite easily.
Here is some code to do that take from here... http://webdesignzo.com/show-featured-images-in-rss-feed-feedburner/ modified a little for your purposes.
function modRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content;
}
$content = $content . '<div class="bottomgift clearfix"><a target="_blank" rel="nofollow" href="' . getCustomField('Affiliate Link') . '" class="go-button">Go</a</div>';
//Adding Custom Fields
$content = $content . get_post_meta($post->ID,'custom_field_name', true);
return $content;
}
add_filter('the_excerpt_rss', 'modRSS');
add_filter('the_content_feed', 'modRSS');
My code is untested but something like this should be what you are looking for.
P.S. This isn't styling per say as your question title suggests, but more adding markup to it.

Related

List WP Categories with Custom field Values

I added a Field Group using ACF plugin to the default WP Category Add/Edit screen so that I can add additional values to categories such as category icon/background image, checkbox to displayed on home or not and assign it as one of the popular categories.
I'm trying to list all the categories on the homepage based on popularity checkbox along with their icon and background image.
I have two image fields such as category_icon for storing category icon and category_bg_image for storing category background image.
Below is the code where I'm able to list the Categories however the custom field values of icon and background image isn't showing.
if (have_posts() ) :
while (have_posts() ) : the_post();
$args=array(
'hide_empty' => '0',
'meta_key' => 'popular_services_category'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<li>' . $category->name . '</li>';
global $post;
$terms = get_the_terms($post->ID, 'category');
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = get_field('category_icon', 'category_' . $term->term_id );
echo '<li>' . $custom_field['url'] . '</li>';
}
}
endwhile;
I was able to solve the issue with little modification and I am posting it here for others to be helpful.
if (have_posts() ) :
while (have_posts() ) : the_post();
$args=array(
'hide_empty' => '0',
'meta_key' => 'popular_services_category'
);
$categories=get_categories($args);
foreach($categories as $category)
{
echo '<li>' . $category->name . '</li>';
$image = get_field('category_icon', $category->taxonomy . '_' .
$category->term_id );
$image_bg = get_field('category_bg_image', $category->taxonomy . '_' . $category->term_id );
// Check if the image exists
if ( $image ) : ?>
<img src="<?php echo $image['url']; ?>" />
<img src="<?php echo $image_bg['url']; ?>" />
endif;
}
endwhile;

WP - shortcode including a mixture of html and php (ACF variables)

I'm trying to create a WP shortcode that would include both html and php. For example, something like that (that does not work):
function my_first_shortcode() {
$content = <<<EOT
<h1>Some title</h1>
<p><?php the_field('description'); ?></p>
EOT;
return $content;
}
add_shortcode('my_shortcode', 'my_first_shortcode');
The the_field('name_of_field'); normally outputs the content of the specified variable/field (Advanced Custom Fields).
Is the HEREDOC way the right way of doing that? If so, how would I do it? It'd be also great if I could pass variables to the shortcode.
Thank you
First, you can't write PHP tags inside HEREDOC.
You can use it like that:
$the_field = 'the_field';
$content = <<<EOT
<h1>Some title</h1>
<p>{$the_field('description')}</p>
EOT;
In order to pass attributes to a shortcode it's very simple.
for example we have the shortcode:
[my_shortcode att_1="some_value" att_2="some_value"]
function my_first_shortcode($atts)
{
$att_1 = $atts['att_1'];
$att_2 = $atts['att_2'];
}
add_shortcode('my_shortcode', 'my_first_shortcode');
I always prefer to use output buffering for my shortcodes, example below.
function my_first_shortcode() {
ob_start(); ?>
<h1>Some title</h1>
<p><?php echo the_field('description'); ?></p>
<?php
return ob_get_contents();
}
add_shortcode('my_shortcode', 'my_first_shortcode');
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
ob_start();
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . esc_url($image['url']) .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
$start_your_application_group = ob_get_clean();
return $start_your_application_group;
}

WordPress - How to create a shortcode that outputs a CPT

There are a handful of things I'd like to do with this shortcode I'm working on. My knowledge about this isn't the best, but I'm trying to learn.
/**
* Recent Project Shortcode
*/
function project_query() {
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$projects_query = new WP_Query( $args );
if ( $projects_query->have_posts() ) :
// var_dump(the_post_thumbnail_url("full")); exit;
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
while ( $projects_query->have_posts() ) :
$projects_query->the_post();
// Do stuff with each post here
$title = get_the_title();
$link = get_the_permalink();
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
endwhile;
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );
There are a few issues here. What does work is that on the front-end it pulls the project name, which is sweet, and the button links to the appropriate page.
Here's how I'd like the shortcode to look when using it: [show_projects posts_per_page="3" order="ASC"] I want to make it "easy" for the user to modify the $args. The second thing that isn't working is the background url I'm trying to do. Right now in the front end everything is outputting except that background url.
Hey Darren the problem is that you create the $featured_img variable after you use it. It should be in while loop.
Please try this code
/**
* Recent Project Shortcode
**/
function project_query($atts) {
$atts = shortcode_atts(
array(
'example_attribute' => 'example_value',
),
$atts, 'example'
);
//if you want to use the attribute you should use $atts['example_attribute'] for example
$args = array(
'posts_per_page' => 1,
'post_type' => 'projects',
'order' => 'ASC',
);
$posts = get_posts( $args );
if ( !empty( $posts ) ) :
$post = array_shift( $posts );
$title = get_the_title($post->ID);
$link = get_the_permalink($post->ID);
$featured_img = get_the_post_thumbnail_url( $post->ID, 'full' );
$html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">';
// Do stuff with each post here
$html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>';
$html_out .= '</article>';
else : // No results
echo "Nothing to show";
endif;
return $html_out;
}
add_shortcode( 'show_project', 'project_query' );

<!--more--> being ignored when WordPress content is displayed

I'm trying to write a plugin that will take page id's and return a preview of the page. Here's my code:
function page_preview($atts,$pageid = null) {
extract(shortcode_atts(array(
"pageid" => '0'
), $atts));
$the_query = new WP_Query( 'page_id=' . $pageid . '' );
global $more;
$more = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content=get_the_content();
}
}
return '<div class="callout">' .
'<h4>' .
$title .
'</h4>' .
$thumbnail .
$content . '<a href="' .
get_the_permalink($pageid) .
'">Continue reading</a></div>';
}
add_shortcode( 'pagepreview', 'page_preview' );
and it gets called on wpadmin editor like so:
[pagepreview pageid=11][pagepreview pageid=13][pagepreview pageid=8054]
i.e., that would display a page preview for each of those page ids.
The "more" doesn't work.
global $more;
$more = 0;
usually fixes this issue, but it's not in my case. Can anyone see what I"m doing wrong? Thanks.
You're getting the full content because
$content=get_the_content();
is not applying the_content() filters, and $more=0 must be on a line after $the_query->the_post(); inside the while loop. Change your while loop to this:
while ( $the_query->have_posts() ) {
$the_query->the_post();
$more=0;
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
}
See get_the_content and read more in pages on the WordPress Codex for where I sourced this.

Returning an image url from its attachment ID using a custom post type

I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}

Resources