Wordpress get content of page (with php inside) - php

I would like to be able to have the html content of a wordpress page. I try to use this kind of code :
$my_id = 5369;
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
But it returns empty an empty string whereas when I access to the page, the content is not empty. Indeed, there is php code inside the content of the page I would like to get. So, how I can do to get the content of this page knowing that there is php code inside?
data content :
<div id="content" class="oe">
<?php $loop = new WP_Query( array('post_type'=>'mcm')); ?>
<div id="slid" class="div-slider">
<?php
/* get value */
$appercu_collection = get_post_custom_values ( "o" );
if ($appercu_collection) { // si appercu
class Image {
public $link;
public $image;
}
/* init value */
$imageCollection = array ();
/* complet collection */
foreach ( $appercu_collection as $img ) {
$img_tmp = new Image ();
$img_tmp->image = wp_get_attachment_image_src( $img , 'medium' )[0];
array_push ( $imageCollection, $img_tmp );
}
display_slider ( $imageCollection );
}
?>
</div>
<div id="content-meta">
<div class="meta-detail-header"><?php echo get_post_custom_values ( "_at" )[0]; ?></div>
<div class="meta-detail-title"><?php the_title(); ?></div>
<div class="audio">
<div id="audio-inner">
<?php $au = get_post_custom_values("_au");
if ($au) {
$urlguid = wp_get_attachment_url ( $au [0] );
if ($urlguid) {
display_audio ( $urlguid );
}
}
?>
</div>
</div>
</div>
<div id="ex">
<?php
$newdebut = get_long_formatted_date( $date_debut[0] );
$newfin = get_long_formatted_date( $date_fin[0] );
?>
<div id="expo-temp-dates"><?php echo sprintf( __( 'From %s to %s' , 'wptouch-pro' ) , $newdebut , $newfin );?></div>
<?php
$tar = get_post_custom_values("_tarif");
if($tar) { ?>
<div id="exprice"><?php echo $tar[0]; ?></div>
<?php }?>
</div>
<div id="content-o" >
<!-- GET Content -->
<div id="content-description" class="detail-container">
<?php display_zoom_controls(); ?>
<?php echo wpautop( wptouch_the_content() ); ?>
</div>
<!-- GET BONUS -->
<div id="content-bottom-sticky">
<a href="<?php echo $url_guide;?>"
class="tappable-button tappable dark" role="Button"><?php _e( 'TUIDE' , 'wptouch-pro' ); ?></a>
</div>
</div>
<?php endif; ?>
<!-- inside intervalle -->
<?php endwhile;
wp_reset_postdata();?>
<?php if( ! $content) { ?>
<div id="title" class="title">
<h2><?php _e( 'N' , 'wptouch-pro' ); ?></h2>
</div>
<?php }?>

I finally resolve my problem by doing an ajax request to my page and get the content thanks to
wp_remote_get . Indeed, It was a page with a custom type post and custom fields.
Here is my code :
$resp = wp_remote_retrieve_body( wp_remote_get($url,$option));
$doc = new DOMDocument();
$doc->loadHTML($resp);
$element = $doc->getElementById('content');
$content = $doc->saveHTML($element);
$content contains the html content of the element with the id "content" of the page whose url is $url.

Related

Add links to images gallery

I need to link some gallery images to different external webistes. After some research I'm not able to found a solution that isn't using a plugin. Is this possible in wordpress without using plugins? I can't install plugins for this project so any suggestion will be appreciated thanks.
Here is my code:
$args = array(
'post_type' => 'post',
'name' => 'partners'
);
$logo_img = new WP_Query( $args );
?>
<div class="container-fluid" id="">
<div class="row" style="margin-top:1em;margin-bottom:1em;">
<?php if( $logo_img->have_posts() ): while( $logo_img->have_posts() ): $logo_img->the_post();
$logo_gallery = get_post_gallery_images( $post->ID );
if( $logo_gallery ): ?>
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<?php foreach($logo_gallery as $logo ): ?>
<img class="img-fluid" src="<?php echo $logo; ?>" alt="" width="60" id="partner-logo" style="margin:0 .5em 0 .5em;"/>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
Depending on how many images we are talking about, if these images are named you could use some logic to create a URL based on the name, if they are not named and there aren't that many, you could rename them.
Here's an example;
<?php
// If there is a matching string, set a specific URL
if (preg_match('/site1/',$logo)) { $set = "yes"; $link = "https://website1.com"; }
if (preg_match('/site2/',$logo)) { $set = "yes"; $link = "https://website2.com"; }
if (preg_match('/site3/',$logo)) { $set = "yes"; $link = "https://website3.com"; }
// If there is no matching string, set a default URL
if ($set !== "yes") { $link = "https://default.com"; }
// Now encase your image in a URL
echo "<a href='$link'>
<img class='img-fluid' src='$logo' alt='' width='60' id='partner-logo' style='margin:0 .5em 0 .5em;'/>
</a>";
?>
By the way, id='partner-logo' - the id should be unique.

Change HTML for odd & even items in a foreach loop

My PHP isn't great!
I have some metaboxes in a wordpress website that displays a column type layout. I can output the metabox content as follows:
<?php
// Group ID
$columns_values = rwmb_meta( 'columns_solutions' );
if ( ! empty( $columns_values ) ) : ?>
<section class="columns">
<?php foreach ( $columns_values as $columns_value ) {
// Grab the image
$columns_imgs = isset( $columns_value['_rtl_column_solutions_image'] ) ? $columns_value['_rtl_column_solutions_image'] : array();
foreach ( $columns_imgs as $columns_img ) {
// Set each image size for the responsive background
$column_image_lg = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-lg' ) );
$column_image_md = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-md' ) );
$column_image_sm = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-sm' ) );
$column_image_xs = RWMB_Image_Field::file_info( $columns_img, array( 'size' => 'column-xs' ) );
}
// Grab the title, oversized text and general text
$column_title = isset( $columns_value['_rtl_columns_title'] ) ? $columns_value['_rtl_columns_title'] : array();
$column_oversized = isset( $columns_value['_rtl_columns_oversized'] ) ? $columns_value['_rtl_columns_oversized'] : array();
$column_general_txt = isset( $columns_value['_rtl_columns_general_text'] ) ? $columns_value['_rtl_columns_general_text'] : array();
?>
<div class="columns-wrapper">
<div class="column">
<?php if(!empty($column_title)) { ?>
<div class="section-title">
<h2>
<?php echo $column_title; ?>
</h2>
</div>
<!-- /.section-title -->
<?php } ?>
<?php if(!empty($column_oversized)) { ?>
<div class="oversized">
<?php echo $column_oversized; ?>
</div>
<!-- /.oversized -->
<?php } ?>
<?php if(!empty($column_general_txt)) { ?>
<?php echo wpautop($column_general_txt); ?>
<?php } ?>
</div>
<!-- /.column -->
<div class="column">
<div class="image min-height cover bg-responsive" style="background-image: url(<?php echo LAZY_IMG; ?>);"
data-lg="<?php echo $column_image_lg['url']; ?>"
data-md="<?php echo $column_image_md['url']; ?>"
data-sm="<?php echo $column_image_sm['url']; ?>"
data-xs="<?php echo $column_image_xs['url']; ?>">
</div>
<!-- /.image -->
</div>
<!-- /.column -->
</div>
<!-- /.column-wrapper -->
<?php } ?>
</section>
<!-- /.columns -->
<?php endif; ?>
Output as below:
This is a clonable meta group, I would like to be able to swap the content and the image (left & right) with each odd and even foreach loop/array.
I feel so stupid, all I needed was to count the posts and output alternate html.
I wrapped my code above in this:
<?php $counter = "";
foreach ( $columns_values as $columns_value ) {
$counter +=1;
// all my var stuff here
?>
<?php if($counter == 1) { ?>
<div class="columns-wrapper odd">
<!-- HTML here for the first loop -->
</div>
<?php }
// If the second item in the loop
elseif($counter == 2) {
// Reset the counter
$counter = 0;
?>
<div class="columns-wrapper even">
<!-- Other HTML here for the second loop -->
</div>
<?php } //end the elseif $counter ?>
<?php } //end the foreach stuff ?>

Define number of images per row in PHP generated image gallery?

Currently my image gallery has 4 images per row. If the screen is minimized below the width of those 4 images, one image will drop to the next line and there will be a line break before the next row. Is there any way to make gallery continuous instead of having the break in images when the screen is resized? Ideally, I would like to start with 5 images per row, then if the viewer has a smaller screen, it will automatically adjust the number of images per row to fit whatever size window they are using.
Here is a link to the gallery: http://rabbittattoo.com/?gallery=gallery
And the PHP:
$pp_gallery_style = get_option('pp_gallery_style');
if($pp_gallery_style == 'f')
{
include_once(TEMPLATEPATH.'/gallery-f.php');
exit;
}
if(!isset($hide_header) OR !$hide_header)
{
get_header();
}
$caption_class = "page_caption";
$portfolio_sets_query = '';
$custom_title = '';
if(!empty($term))
{
$portfolio_sets_query.= $term;
$obj_term = get_term_by('slug', $term, 'photos_galleries');
$custom_title = $obj_term->name;
}
else
{
$custom_title = get_the_title();
}
/**
* Get Current page object
**/
$page = get_page($post->ID);
/**
* Get current page id
**/
if(!isset($current_page_id) && isset($page->ID))
{
$current_page_id = $page->ID;
}
if(!isset($hide_header) OR !$hide_header)
{
?>
<div class="wrapper_shadow"></div>
<div class="page_caption">
<div class="caption_inner">
<div class="caption_header">
<h1 class="cufon"><?php echo the_title(); ?></h1>
</div>
</div>
</div>
</div>
<!-- Begin content -->
<div id="content_wrapper">
<div class="inner">
<!-- Begin main content -->
<div id="gallery_wrapper" class="inner_wrapper portfolio">
<div class="standard_wrapper small">
<br class="clear"/><br/>
<?php
}
else
{
echo '<br class="clear"/>';
}
?>
<?php echo do_shortcode(html_entity_decode($page->post_content)); ?>
<!-- Begin portfolio content -->
<?php
$menu_sets_query = '';
$portfolio_items = 0;
$portfolio_sort = get_option('pp_gallery_sort');
if(empty($portfolio_sort))
{
$portfolio_sort = 'DESC';
}
$args = array(
'post_type' => 'attachment',
'numberposts' => $portfolio_items,
'post_status' => null,
'post_parent' => $post->ID,
'order' => $portfolio_sort,
'orderby' => 'date',
);
$all_photo_arr = get_posts( $args );
if(isset($all_photo_arr) && !empty($all_photo_arr))
{
?>
<?php
foreach($all_photo_arr as $key => $portfolio_item)
{
$image_url = '';
if(!empty($portfolio_item->guid))
{
$image_id = $portfolio_item->ID;
$image_url[0] = $portfolio_item->guid;
}
$last_class = '';
$line_break = '';
if(($key+1) % 4 == 0)
{
$last_class = ' last';
if(isset($page_photo_arr[$key+1]))
{
$line_break = '<br class="clear"/><br/>';
}
else
{
$line_break = '<br class="clear"/>';
}
}
?>
<div class="one_fourth<?php echo $last_class?>" style="margin-right:24px;margin-bottom:24px;margin-top:-20px">
<a title="<?php echo $portfolio_item->post_title?>" href="<?php echo $image_url[0]?>" class="one_fourth_img" rel="gallery" href="<?php echo $image_url[0]?>">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/timthumb.php?src=<?php echo $image_url[0]?>&h=370&w=350&zc=1" alt=""/>
</a>
</div>
<?php
echo $line_break;
}
//End foreach loop
?>
<?php
}
//End if have portfolio items
?>
</div>
<!-- End main content -->
<br class="clear"/><br/>
</div>
<?php
if(!isset($hide_header) OR !$hide_header)
{
?>
</div>
<!-- End content -->
<?php get_footer(); ?>
<?php
}
?>
Thank you in advance for you help!

Get Tweets and Display them in Wordpress

All,
I have the following code to retrieve some Tweets:
if( ! $tweet ) {
$url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name={$username}&count={$how_many}";
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$contents = curl_exec( $curl );
curl_close( $curl );
if ( ! empty( $contents ) ) {
// Decode it.
$tweet = json_decode( $contents );
}
}
// Check to make sure we have a tweet and display it.
if ( $tweet ) {
do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
$output = 'Twitter timed out.';
}
return $output;
I then have the following code to put them in a Slider:
function themeblvd_twitter_slider_default( $tweet, $options, $username, $slider_id ) {
$tweet = (array)$tweet;
$classes = themeblvd_get_classes( 'slider_standard', true );
$classes .= ' hide-nav_standard';
$classes .= ' show-nav_arrows';
$classes .= ' show-pause_play';
//themeblvd_twitter_slider_js( $slider_id, $options );
?>
<div id="tb-slider-<?php echo $slider_id; ?>" class="slider-wrapper standard-slider-wrapper">
<div class="slider-inner<?php echo $classes; ?>">
<div class="slides-wrapper slides-wrapper-<?php echo $slider_id; ?>">
<div class="slides-inner">
<div class="slider standard-slider flexslider">
<div class="tb-loader"></div>
<ul class="slides">
<?php
foreach($tweet as $t){
?>
<li class="slide tight <?php echo $classes; ?>">
<div class="slide-body">
<div class="grid-protection">
<?php
echo '<span class="tweet-icon '.$options['icon'].'"></span>';
echo '<a href="http://twitter.com/'.$username.'/status/'.$t->id_str.'" target="_blank">';
echo $t->text;
echo '</a>';
?>
</div><!-- .grid-protection (end) -->
</div><!-- .slide-body (end) -->
</li>
<?php
}
?>
</ul>
</div><!-- .slider (end) -->
</div><!-- .slides-inner (end) -->
</div><!-- .slides-wrapper (end) -->
</div><!-- .slider-inner (end) -->
<div class="design-1"></div>
<div class="design-2"></div>
<div class="design-3"></div>
<div class="design-4"></div>
</div><!-- .slider-wrapper (end) -->
<?php
}
?>
This code works about 90% of the time and grabs the tweets based on a username etc. However, about 10% of the time it just rotates without displaying any of the texts. Does anyone see anything or a way that I can make this solution better so that the tweets are always displayed? Is there anything from an error handling perspective that I'm missing that I could add to not display it if it can't retrieve the tweets?
Thanks for any advice in advance.
You could try to check if what twitter returns actually is a list of tweets. At the moment you only check if twitter returns anything at all.
// Check to make sure we have a tweet and display it.
if ( is_array($tweet) && isset($tweet[0]->id_str) ) {
do_action( 'themeblvd_twitter_slider', $tweet, $options, $username, $slider_id );
} else {
$output = 'Twitter timed out.';
}

How to edit php file and return wordpress blog entries in multiple columns?

I am using the same theme as this site http://foreignpolicydesign.com/v3/. However, my test site does not distribute the blog entries in columns
What might be causing this problem? I am suspecting it is whatever sets the $col_class to set the x1 ~ xN, but I cannot find the source of this variable.
Here is the code:
<?php include (TEMPLATEPATH . '/tanzaheader.php');
// [grid column setting]
$col_w = 200; // width of grid column
$gap_w = 35; // padding + margin-right (15+15+5)
$max_col = 5; // max column size (style div.x1 ~ xN)
// * additional info *
// check also "style.css" and "header.php" if you change $col_w and $gap_w.
// - style.css:
// div.x1 ~ xN
// div.grid-item
// div.single-item
// ... and maybe #sidebar2 li.widget.
// - header.php:
// gridDefWidth in javascript code.
//
// if you want to show small images in main page always, set $max_col = 1.
// [grid image link setting]
$flg_img_forcelink = true; // add/overwrite a link which links to a single post (permalink).
$flg_img_extract = false; // in single post page, extract thumbnail link to an original image.
$flg_obj_fit = 'large-fit'; // none | small-fit | large-fit ... how to fit size of object tag.
// * additional info *
// if you use image popup utility (like Lightbox) on main index, set $flg_img_forcelink = false;
?>
<!-- <?php if (is_singular()) : $is_top_single = true; /* wide column for single post */ ?> -->
<?php /* make a new query for grid items (in single page) */
$new_query_arg = 'paged='.$paged;
// use this code if you want filter items by category.
$arr_catID = array(20);
foreach( get_the_category() as $cat) $arr_catID[] = $cat->cat_ID;
if ( count($arr_catID) ) $new_query_arg .= '&cat=' . join(',', $arr_catID);
query_posts($new_query_arg);
?>
<!-- <?php endif; /* end of if is_singular() */ ?> -->
<div id="grid-wrapper">
<?php if (have_posts()) :
if ( $is_top_single ) $GLOBALS['more'] = false; //important
while (have_posts()) : the_post(); ?>
<?php
$content = get_the_content('Details ยป');
$content = apply_filters('the_content', $content);
list($col_class, $grid_img) = adjust_grid_image(
$content,
$col_w,
$gap_w,
$max_col,
$flg_img_forcelink,
$flg_obj_fit
);
?>
<div <?php post_class('grid-item ' . $col_class); ?> id="post-<?php the_ID(); ?>">
<h2 class="post-title"><?php the_title(); ?></h2>
<?php if ($grid_img) echo '<div class="grid-image">' . $grid_img . '</div>'; ?>
<div class="post-body">
<?php
$content = preg_replace('/<img(?:[^>]+?)>/', '', $content); // remove img tags
$content = preg_replace('/<a([^>]+?)><\/a>/', '', $content); // remove empty a tags
$content = preg_replace('/<p([^>]*?)><\/p>/', '', $content); // remove empty p tags
$content = preg_replace('/<object(.+?)<\/object>/', '', $content); // remove object tags
echo $content;
?>
</div>
<p class="post-meta">
Published on <?php the_time( get_option('date_format') ); ?> <?php the_time(); ?>.<br />
Filed under: <?php the_category(', ') ?> <?php the_tags('Tags: ', ', '); ?>
<?php edit_post_link(__("Edit This"), '(', ')'); ?><br />
<?php /*comments_popup_link();*/ ?>
</p>
</div>
<?php endwhile; else : ?>
<div class="grid-item x1">
<h2>Not Found</h2>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
<?php endif; ?>
</div><!-- /grid-wrapper -->
<div class="pagination" id="grid-pagination">
<?php paginate_links2($is_top_single); ?>
</div>
<?php /* reset the query */
wp_reset_query();
?>
</div><!-- /container -->
<?php include (TEMPLATEPATH . '/tanzafooter.php'); ?>
The columns are not created by PHP code, they are created dynamically by a jQuery plugin.
Your theme is referencing it here:
http://wpbeta.nfshost.com/wp-content/themes/js/jquery.vgrid.0.1.4-mod.js
But that returns a 404 file not found error.

Categories