I'm pulling out the data from my CRM to my WordPress site using a XML.
It worked actually, but I want to add some functions like read more with the my XML data {Web_Remarks[1]} it contains a long description.
Then I tried to add conditions from from my function.php
like excerpt and edit my descriptions code.
For my functions.php I add
<?php
// Customize excerpt for description word count length
function custom_excerpt_length(){
return 25;
}
add_filter('excerpt_length','custom_excerpt_length');
?>
And for my description.php I change my <?php the_content();?> to
<?php if ($post->post_excerpt) { ?>
<p>
<?php echo get_the_excerpt(); ?>
Read MoreĀ»
</p>
<?php }else{
the_content();
}
?>
I expect the output should be "3Bed room with laundry ro..Read More..."
but what I have now is
"3Bed room with laundry room and sea view with 2 car parkings only for 3.06M!
-Bright
-Spacious
-Sea/Palm view
-Balconies
-Higher floor
-No Construction chance infront
-Prime location
real estate are a Property Investment Firm with an ownership of more than 2500 properties all around the world.These are handpicked exclusive apartments and villas, located within the most prestigious and high-profile developments of Dubai. We do not just believe in customer satisfaction, we aim for customer delight. We understand that our customers define the standard of quality and service and your loyalty must be earned."
Try,
<?php
$excerpt = get_the_excerpt();
if( !empty($excerpt) ) { ?>
<p>
<?php echo $excerpt; ?>
Read MoreĀ»
</p>
<?php
} else {
the_content();
}
Or this will be a another method you can use:
<?php
// Add this code in functions.php file
function wp_get_custom_excerpt( $limit = '25' ) {
global $post;
$content = get_the_excerpt();
if( empty($content) ) {
$content = strip_shortcodes( $post->post_content );
}
$excerpt = wp_trim_words( $content, $limit, '[...]' );
return $excerpt;
}
// Remove your excerpt code and just echo function.
echo wp_get_custom_excerpt();
Related
I have a Wordpress site, and I want to set up a paywall for certain content.
I would like to show some intro of each such article for Google index and users as well, but rest of it would be hidden for guests and available for logged in (in my case paid users).
I successfully implemented is_user_logged_in() PHP statement from Wordpress, but I am not finding PHP code anywhere online about hiding everything after n-characters, or words.
My intended workflow is:
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
echo 'show only 200 characters or words of the content code should be here';
}
?>
You can use WordPress default function wp_trim_words.
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content(); // $content is whatever your content field.
echo wp_trim_words( $content, 200, ' ' );
}
?>
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content();
//If your template has some other way of getting the post content then use that variable.
echo substr($content, 0, strpos($content, ' ', 200));
}
?>
I'm using the below funky bit of code in my functions.php to enable the execution of PHP in my sidebar:
// 12. Execute/Parse PHP in Sidebar Content
// =============================================================================
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
I'm then using this code in my sidebar to reference a list of custom fields (related articles) that I want to list in the sidebar:
<?php $post_objects = get_field( 'related_article_list'); ?>
<?php if ( $post_objects) { ?>
<ul>
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php } ?>
Now this code works fine in my page template and correctly lists the three related articles that I have set in each post, but in the sidebar it just generates the current post title three times.
Any ideas much appreciated!
It is generally not good practice to place PHP code directly in a sidebar textfield then parsing/executing its content is leaves open to many potential problem.
Instead you should place the code inside a shortcode and then place that shortcode tag inside the Widget in question. [my_related_article_list]
add_shortcode('my_related_article_list','my_related_article_list_func');
function my_related_article_list_func(){
$post_objects = get_field( 'related_article_list');
$html = '';
if ( $post_objects) {
$html = '<ul>';
foreach( $post_objects as $post):
$html .= '<li>'.get_the_title($post).'</li>';
endforeach;
$html .= '</ul>';
}
return $html;
}
Concerning the fact that you see the current post three time in the sidebar has todo with the loop not have been properly set and 3 times because you probably also pulled the revisions.(i suppose)
Maybe executing the code in a shortcode could help improve your predicament in solving parts of the problem or even the whole problem.
Edit: Using the setup_postdata is unecessary since calling get_* functions which returns data instead of outputting it straight based on the current loop data set by setup_postdata. Therefor calling the get_* with the $post argument will return the appropriate context.
Solved by #Musk using the following shortcode:
add_shortcode('my_related_article_list','my_related_article_list_func');
function my_related_article_list_func(){
$post_objects = get_field( 'related_article_list');
$html = '';
if ( $post_objects) {
$html = '<ul>';
foreach( $post_objects as $post):
$html .= '<li>'.get_the_title($post).'</li>';
endforeach;
$html .= '</ul>';
}
wp_reset_postdata();
return $html;
}
I am using a function that returns the content for specific posts of type 'product.' However, I want to check the length of the content, and if it is under a certain length, I would like to add a class to center the content (I have it left-aligned by default). I understand how to write the IF statement to check the length (so I left it out of the example). But I am having an issue returning the content with the class. Here is the function.
function post_product_description() {
$product_title = get_the_title();
$the_query = new WP_Query( array(
'post_type' => 'product',
'name' => $product_title
) );
while ( $the_query->have_posts() ) : $the_query->the_post();
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
//$content = '<span class="centered">'.$content.'</span>';
endwhile;
wp_reset_postdata();
return $content;
}
add_shortcode('product_description', 'post_product_description');
If I just return the content as
return $content
it outputs the content in a <p> tag just fine like this:
<p>the content text here</p>
However, if the length of the content exceeds a certain number, I want the <span> tag to output around the text like this:
<span class="centered">the content text here</span>
The problem is, if I try to append the <span> tags to my $content variable, as I am doing in the commented line in the original code, wordpress outputs like this:
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>
How do I return $content without it adding all of those extras <p> tags and with it wrapping the <span> tag around the content?
maybe you are looking for something like this:
functions.php
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() ):
$length = strlen($content);
if ($length> 500) //
{
$content = sprintf('<span class="custom-class">' .$content. '</span>');
}
endif;
// Returns the content.
return $content;
}
This code will output the content wrapped inside span class="custom-class" you can check more about it at the Wordpress codex :
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
So I have the code below for my website in WordPress. Under that it shows the title of the current page for example, mine is "Baby"(in black). So I would like to have a unique class or ID for each title (so I can have a different color for each title instead of black for all) but since they are generated by PHP I do not know to to achieve this. If it was a pure HTML I could just give a class and style it but now I do not have any unique class.
<?php get_header(); ?>
<div class="primary-content">
<?php
// display taxonomy info
themedsgn_biancca_taxonomy_info();
// initiate the blogroll type
$blogroll_type = get_theme_mod( 'themedsgn_setting_archive_layout', 'grid' );
if( have_posts() ) {
echo '<div class="blogroll blogroll-' . $blogroll_type . ' cf">';
while( have_posts() ) {
the_post();
get_template_part( 'content', $blogroll_type );
}
echo '</div>';
// pagination
themedsgn_biancca_post_pagination();
} else {
get_template_part( 'content', 'none' );
}
?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Look in Appearance/Editor, then look for the file called archive.php. Then search for archive-title. It should be in there.
I noticed something odd when I switch themes. In garland, I can see the view & 'edit panel' tab buttons; but when I switch back to my custom theme, it disappears.
I already have the tabs line:
<?php if ($tabs): print '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
But it's just not appearing. Why is that?
Here is some of the code for page.tpl.php:
<div class="main-container">
<div class="mcontent">
<div id="content-header">
<?php if ($mission): print '<div id="mission">'. $mission .'</div>'; endif; ?>
<?php if ($tabs): print '<div id="tabs-wrapper" class="clear-block">'; endif; ?>
<?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
<?php if ($tabs): print '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
<?php if ($tabs2): print '<ul class="tabs secondary">'. $tabs2 .'</ul>'; endif; ?>
<?php if ($show_messages && $messages): print $messages; endif; ?>
<?php print $help; ?>
</div> <!-- /#content-header -->
<?php print $content; ?>
</div>
</div>
I've used that code in quite a few custom themes without any issues so far.
It's hard to know without seeing the site/your code, but a couple of possibilities:
clearing your cache (Performance > Clear site cache)
making sure the $tabs variable is in page.tpl.php, not node.tpl.php etc.
Other things it could be (but maybe not if other themes are working):
your user doesn't have permissions to use that input format (PHP code, Full HTML etc)
the session isn't working or you've been logged out while on those pages (of course you will see this right away if, for example, your admin menu shows up. I've seen it happen on some setups, though)
Usually, either the code is in the wrong place, it's being hidden/obscured (by CSS/page formatting), or the person doesn't have the permissions to view it.
If none of the above work, you may want to rebuild permissions (in Drupal 6, Content Management > Post Settings > Rebuild Permissions), or play with enabling existing permissions to see if that's the culprit.
I hired someone to fix the issue, here is what is done:
<?php
/**
* Override of theme_menu_local_tasks().
* Add argument to allow primary/secondary local tasks to be printed
* separately. Use theme_links() markup to consolidate.
*/
function kidstoria_menu_local_tasks($type = '') {
if (module_exists('ctools')) {
if ($primary = ctools_menu_primary_local_tasks()) {
$primary = $primary;
}
if ($secondary = ctools_menu_secondary_local_tasks()) {
$secondary = $secondary;
}
}
else
{
if ($primary = menu_primary_local_tasks()) {
$primary = $primary;
}
if ($secondary = menu_secondary_local_tasks()) {
$secondary = $secondary;
}
}
switch ($type) {
case 'primary':
return $primary;
case 'secondary':
return $secondary;
default:
return $primary . $secondary;
}
}